package com.css.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.css.dao.ICarDAO;
import com.css.vo.Car;

public class CarDAO extends HibernateDaoSupport implements ICarDAO {

	@Override
	public void save(Car car) {
		getHibernateTemplate().save(car);
	}

	@Override
	public void delete(Car car) {
		getHibernateTemplate().delete(car);
	}

	@Override
	public void update(Car car) {
		getHibernateTemplate().update(car);
	}

	@Override
	public Car find(String carModel) {
		List<Car> list = getHibernateTemplate().find(
				"from Car where CarModel = ?", carModel);
		if (list.size() > 0) {
			return (Car) list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<Car> findAllCar() {
		List<Car> list = getHibernateTemplate().find("from Car");
		return list;
	}

	@Override
	public List<Car> findById(Integer id) {
		List<Car> list = getHibernateTemplate().find(
				"from Car where CarId = ?", id);
		return list;
	}

	@Override
	public List<Car> searchCar(String keyword) {
		try {
			String hql = "from Car where carModel like ? or version like ? or year like ? or color like ?";
			String param = "%" + keyword + "%";
			Object[] params = new Object[] { param, param, param, param };
			return getHibernateTemplate().find(hql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Car>();
		}
	}

}
